package com.taojin.shoppinginterest.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.taojin.shoppinginterest.R;


public class PopHelper_welfare {
    private CallBack callBack;

    private static PopHelper_welfare helper;
    private PopupWindow popupWindow;

    public static PopHelper_welfare getInstance() {
        if (helper == null) {
            helper = new PopHelper_welfare();
        }
        return helper;
    }

    private void init(Context context) {
        popupWindow = new PopupWindow(context);
        popupWindow.setBackgroundDrawable(new ColorDrawable(R.color.white));
    }

    private void init(Context context, int width, int height) {
        this.init(context);
        popupWindow.setHeight(height);
        popupWindow.setWidth(width);
    }

    public void init(Context context, View contentView, View anchor, int width, int height) {
        popupWindow = new PopupWindow(context);
        //设置popupwindow高度
        popupWindow.setHeight(height);
        //设置popupwindow宽度
        popupWindow.setWidth(width);
        popupWindow.setFocusable(true);
        //设置popupwindow的显示界面
        popupWindow.setBackgroundDrawable(new ColorDrawable(R.color.yellow));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setContentView(contentView);
        //设置popupwindow显示的父控件
        popupWindow.showAsDropDown(anchor);
    }

    public void init(Context context, View anchor, CallBack callBack) {
        this.init(context, createContentView(context), anchor, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.callBack = callBack;
    }

    private View createContentView(final Context context) {

        LayoutInflater inflate = LayoutInflater.from(context);
        View contentView = inflate.inflate(R.layout.pop_listview_welfare, null);
        ListView listView = (ListView) contentView.findViewById(R.id.listview_pop_welfare);
        listView.setFocusable(true);
        String[] data = new String[]{"默认排序", "即将结束"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, android.R.id
                .text1, data);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                callBack.getPosition(position + 1);
                Toast.makeText(context, position + 1 + "", Toast.LENGTH_LONG).show();
            }
        });
        return contentView;
    }

    public void dismiss() {
        if (popupWindow != null) {
            popupWindow.dismiss();
        }
    }

    public boolean isShowing() {
        return popupWindow != null && popupWindow.isShowing() ? true : false;
    }

    public interface CallBack {
        void getPosition(int position);
    }

}
